package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.ApplyCreatorsConstant;
import org.news.web.constant.CategoriesStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.ApplyCreatorsVo;
import org.news.web.domain.bo.ApplyCreatorsBo;
import org.news.web.service.IApplyCreatorsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作者申请审核功能
 *
 * @author Gu Ci
 * @date 2024-03-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/applyCreators")
public class ApplyCreatorsController extends BaseController {

    private final IApplyCreatorsService applyCreatorsService;

    /**
     * 查询作者申请审核功能列表
     */
    @SaCheckPermission("web:applyCreators:list")
    @GetMapping("/list")
    public TableDataInfo<ApplyCreatorsVo> list(ApplyCreatorsBo bo, PageQuery pageQuery) {
        return applyCreatorsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出作者申请审核功能列表
     */
    @SaCheckPermission("web:applyCreators:export")
    @Log(title = "作者申请审核功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApplyCreatorsBo bo, HttpServletResponse response) {
        List<ApplyCreatorsVo> list = applyCreatorsService.queryList(bo);
        ExcelUtil.exportExcel(list, "作者申请审核功能", ApplyCreatorsVo.class, response);
    }

    /**
     * 获取作者申请审核功能详细信息
     *
     * @param userCreatorsId 主键
     */
    @SaCheckPermission("web:applyCreators:query")
    @GetMapping("/{userCreatorsId}")
    public R<ApplyCreatorsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userCreatorsId) {
        return R.ok(applyCreatorsService.queryById(userCreatorsId));
    }

    /**
     * 新增作者申请审核功能
     */
    @SaCheckPermission("web:applyCreators:add")
    @Log(title = "作者申请审核功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ApplyCreatorsBo bo) {
        return toAjax(applyCreatorsService.insertByBo(bo));
    }

    /**
     * 修改作者申请审核功能
     */
    @SaCheckPermission("web:applyCreators:edit")
    @Log(title = "作者申请审核功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ApplyCreatorsBo bo) {
        return toAjax(applyCreatorsService.updateByBo(bo));
    }

    /**
     * 删除作者申请审核功能
     *
     * @param userCreatorsIds 主键串
     */
    @SaCheckPermission("web:applyCreators:remove")
    @Log(title = "作者申请审核功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userCreatorsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userCreatorsIds) {
        return toAjax(applyCreatorsService.deleteWithValidByIds(List.of(userCreatorsIds), true));
    }
    /**
     *
     */
    @GetMapping("/applyStatus")
    public R<List<CategoriesStatus>> applyStatusList(){
        return R.ok(ApplyCreatorsConstant.ApplysCategoriesStatuses());
    }


    /**
     * 修改状态
     */
    @PostMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody ApplyCreatorsBo bo){
        return toAjax(applyCreatorsService.changeStatus(bo));
    }
}
